package online.sixfish.doubao.controller;

import online.sixfish.common.api.ApiResult;
import online.sixfish.common.pojo.User;
import online.sixfish.common.utils.SecurityUtil;
import online.sixfish.doubao.model.dto.CommentDTO;
import online.sixfish.doubao.model.entity.Comment;
import online.sixfish.doubao.model.vo.CommentVO;
import online.sixfish.doubao.service.IBmsCommentService;
import online.sixfish.doubao.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/comment")
public class BmsCommentController extends BaseController {

    @Resource
    private IBmsCommentService bmsCommentService;
    @Resource
    private UserService umsUserService;

    @PreAuthorize("hasAnyRole('user')")
    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByTopicID(@RequestParam(value = "topicid", defaultValue = "1") String topicid) {
        List<CommentVO> lstBmsComment = bmsCommentService.getCommentsByTopicID(topicid);
        return ApiResult.success(lstBmsComment);
    }

    @PreAuthorize("hasAnyRole('user')")
    @PostMapping("/add_comment")
    public ApiResult<Comment> add_comment(@RequestBody CommentDTO dto) {
        String username = SecurityUtil.currentUser().getUsername();
        User user = umsUserService.getUserByUsername(username);
        Comment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }
}
